package com.example.zbh.dsliao.ui.activty;

import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;

import com.easemob.chat.EMChat;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMChatOptions;
import com.easemob.chat.EMContactListener;
import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.db.DatabaseHelper;
import com.example.zbh.dsliao.db.entity.ApplyFriend;
import com.example.zbh.dsliao.model.entity.AddFriend;
import com.example.zbh.dsliao.ui.adapter.NewFriendMsgAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zbh on 15/11/21.
 */
public class NewFriendsMsgActivity extends BaseActivity implements NewFriendMsgAdapter.OnRecyclerItemClickListenter, View.OnClickListener {
    private RecyclerView recyclerView;
    private GridLayoutManager manager;
    private int index;
    private DatabaseHelper helper;
    private NewFriendMsgAdapter newFriendMsgAdapter;
    private Handler handler;
    private LinearLayout back;
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            List<ApplyFriend> friendList = helper.queryFriend();
            newFriendMsgAdapter.addData(friendList);
        }
    };


    @Override
    protected int getLayOut() {
        return R.layout.activity_new_friend_msg;
    }

    @Override
    protected void initView() {
        recyclerView = findeView(R.id.recyclerView);

    }

    @Override
    protected void initData() {

        manager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(manager);
        helper = DatabaseHelper.getInstance();
        newFriendMsgAdapter = new NewFriendMsgAdapter();
        recyclerView.setAdapter(newFriendMsgAdapter);
        back = findeView(R.id.back);
        back.setOnClickListener(this);
        handler = new Handler();


        Log.d("tagggggg", "initData() called with: " + "");
        EMChat.getInstance().setAppInited();
        EMContactManager.getInstance().setContactListener(new EMContactListener() {

            @Override
            public void onContactAgreed(String username) {
                //好友请求被同意
                Log.i("tagggggg", username + "onContactAgreed");



            }

            @Override
            public void onContactRefused(String username) {
                //好友请求被拒绝
                Log.i("tagggggg", username + "onContactRefused");

            }

            @Override
            public void onContactInvited(String username, String reason) {

                //收到好友邀请
                Log.i("tagggggg", username + "onContactInvited");
                index = 0;
                ApplyFriend applyFriend = new ApplyFriend();
                applyFriend.setName(username);
                applyFriend.setIndex(index);
                helper.saveApplyFriend(applyFriend);
                handler.post(runnable);
            }

            @Override
            public void onContactDeleted(List<String> usernameList) {
                //被删除时回调此方法
                Log.i("tagggggg", usernameList.toString());

            }


            @Override
            public void onContactAdded(List<String> usernameList) {
                //增加了联系人时回调此方法
                Log.i("tagggggg", "onContactAdded" + usernameList.get(0));
                helper.deleteFriendByName(usernameList.get(0));
                index = 1;
                ApplyFriend applyFriend = new ApplyFriend();
                applyFriend.setName(usernameList.get(0));
                applyFriend.setIndex(index);
                helper.saveApplyFriend(applyFriend);
                handler.post(runnable);



            }
        });
        Log.i("taggggg", "完毕");
        List<ApplyFriend> list = helper.queryFriend();
        newFriendMsgAdapter.addData(list);
        newFriendMsgAdapter.setOnRecyclerItemClickListenter(this);


    }
    @Override
    public void onClick(View v) {
    switch (v.getId()){
        case R.id.back:
            finish();
            break;
    }
    }

    @Override
    public void onOnRecyclerItemClick(String name) {
        try {
            EMChatManager.getInstance().acceptInvitation(name);
        } catch (EaseMobException e) {
            e.printStackTrace();
        }
    }


}
